using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SoinusLibrary;

namespace HomeLite.View
{
    public partial class FormLogin : Form
    {
        #region EventHandlers
        public event LoginBtnHandler LoginClick;
        public event EventHandler ExitEvent; 
        #endregion

        #region Constructor
        public FormLogin()
        {
            InitializeComponent();
        }

        public FormLogin(String[] lastLogin)
            : this()
        {
            if (lastLogin == null)
            {
                lastLogin = new String[] { };
            }
            if (lastLogin.Length > 0)
            {
                this.cbEmail.Items.AddRange(lastLogin);
                this.cbEmail.Text = (String)this.cbEmail.Items[0];
            }
        } 
        #endregion
                
        #region Events
        private void btLogin_Click(object sender, EventArgs e)
        {
            LoginClick(this, new LoginEventArgs(cbEmail.Text, tbPassword.Text));
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            ExitEvent(this, new ExitEventArgs());
        }

        private void tbPassword_TextChanged(object sender, EventArgs e)
        {
            if ((this.cbEmail.Text.Length != 0) && (this.tbPassword.TextLength != 0))
                this.btLogin.Enabled = true;
            else this.btLogin.Enabled = false;
        }

        private void cbEmail_TextChanged(object sender, EventArgs e)
        {
            if ((this.cbEmail.Text.Length != 0) && (this.tbPassword.TextLength != 0))
                this.btLogin.Enabled = true;
            else this.btLogin.Enabled = false;
        }

        private void FormLogin_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.ExitEvent(this, new ExitEventArgs());
        } 
        #endregion

        #region Methods
        public void SetMessage(String message)
        {
            this.lbLoginResponse.Text = message;
        } 
        #endregion

    }

    public delegate void LoginBtnHandler(Object sender, LoginEventArgs e);

    public class LoginEventArgs : EventArgs
    {
        private string email;
        private string password;

        public string Email
        {
            get { return email; }
            set { email = value; }
        }

        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        public LoginEventArgs(string pEmail, string pPassword)
        {
            this.email = pEmail;
            this.password = pPassword;
        }
    }

}